@extends('layouts.admin.layout')

@section('content')
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">编辑角色</strong> / <small>Edit</small></div>
            </div>

            <hr>
            @include('layouts.admin._flash')
            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form am-form-horizontal" action="{{route('auth.role_update', $role->id)}}" method="post">
                        {!! csrf_field() !!}
                        {{ method_field('PUT') }}
                        <div class="am-form-group">
                            <label for="doc-ipt-1" class="am-u-sm-2 am-form-label">角色英文名</label>
                            <div class="am-u-sm-10">
                                <input type="text" name="name" id="doc-ipt-1" value="{{$role->name}}">
                            </div>
                        </div>

                        <div class="am-form-group">
                            <label for="doc-ipt-3" class="am-u-sm-2 am-form-label">角色中文标识</label>
                            <div class="am-u-sm-10">
                                <input type="text" name="label" id="doc-ipt-3" value="{{$role->label}}">
                            </div>
                        </div>

                        <div class="am-form-group">
                            <label for="doc-ipt-4" class="am-u-sm-2 am-form-label">分配权限</label>
                            <div class="am-u-sm-10">
                                <a href="javascript:void(0)" id="doc-ipt-4" class="am-btn am-btn-primary">全选/取消</a>
                            </div>
                        </div>
                        @foreach($permissions as $permission)
                            <div class="am-form-group permisson" id="{{$permission->id}}">
                                <label  class="am-u-sm-2 am-form-label"></label>
                                <div class="am-u-sm-10">
                                    <div class="am-u-sm-10">
                                        <label>
                                            <input type="checkbox" @if(in_array($permission->id,$role->permissions->pluck('id')->all())) checked @endif class="parent parent{{$permission->id}} permission" name="permission_id[]" value="{{$permission->id}}"> {{$permission->label}}
                                        </label>
                                    </div>
                                </div>
                            </div>
                            @if (count($permission->children->pluck('id')))
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-form-label"></label>
                                    <div class="am-u-sm-9">
                                        <label>
                                            ┣━
                                            @foreach($permission->children as $c)
                                                <input type="checkbox" @if(in_array($c->id,$role->permissions->pluck('id')->all())) checked @endif class="children{{$permission->id}} permission c_permisson" pid="{{$permission->id}}" name="permission_id[]" value="{{$c->id}}"> {{$c->label}}&nbsp;&nbsp;
                                            @endforeach
                                        </label>
                                    </div>
                                </div>
                            @endif
                        @endforeach

                        <div class="am-form-group">
                            <div class="am-u-sm-10 am-u-sm-offset-2">
                                <button type="submit" class="am-btn am-btn-primary">提交</button>
                            </div>
                        </div>
                    </form>
                </div>

            </div>
        </div>
@endsection

@section('js')
    <script>
        $(function () {
            //全选或取消
            var time = 0;
            $("#doc-ipt-4").click(function () {
                time++;
                console.log(time);
                if(time%2 != 0){
                    $(".permission").prop("checked",true);
                }else
                    $(".permission").prop("checked",false);
            });
            //选父级时子级全选或者不选是全不选
            $(".parent").click(function () {
                var value = $(this).prop("checked");
                $(".children" + $(this).val()).prop("checked",value);
            });

            $(".c_permisson").click(function () {
                var pid = $(this).attr("pid");
                $(".parent" + pid).prop("checked",true);
                var len = $(".children" + pid + ":checked").length;
                if(len == 0){
                    $(".parent" + pid).prop("checked",false);
                }
            })
        })
    </script>
@endsection